// index.js
const util = require('../../../util/util.js')
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    dataTable: [], // 原始车辆数据
    carTypeCounts: [], // 分类统计结果
    timeStart: null, //时分秒，根据需要选择
    CLIENT_NO: '',
    ClientName: '',
    index: 0,
    clientNoList: [],
  },
  onLoad: function () {
    var that = this;
    const currentDate = new Date();
    currentDate.setDate(currentDate.getDate() - 1);
    var CLIENT_NO = wx.getStorageSync("CLIENT_NO");
    that.setData({
      timeStart: util.formatDate(currentDate),
      CLIENT_NO: CLIENT_NO
    })

    this.getClientNoListInfo()
  },

  // 获取组织机构信息
  getClientNoListInfo() {
    const pickerData = [{
      "label": "全市"
    },{
      "label": "中心城区"
    },{
      "label": "滨海新区"
    },{
      "label": "新五区"
    }]
    this.setData({
      clientNoList: pickerData,
      ClientName: pickerData[0].label // 默认选中第一个
    });
    this.getCarRealtimeInfo()
  },
  // 获取信息
  getCarRealtimeInfo() {
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetVehicleOnlineInfo',
      data: {
        ClientNo: '00000',
        Name: this.data.ClientName,
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // console.log(res.data)
        // const originalData = res.data.slice(1);
        const originalData = res.data;


        // // 分类统计（增加去空格处理）
        // const counts = originalData.reduce((acc, cur) => {
        //   const type = cur.CAR_TYPE.trim(); // 去除前后空格防止统计错误
        //   acc[type] = (acc[type] || 0) + 1;
        //   return acc;
        // }, {});

        // 转换为数组并按数量排序
        this.setData({
          // carTypeCounts: Object.keys(counts)
          //   .map(type => ({
          //     type,
          //     count: counts[type]
          //   }))
          //   .sort((a, b) => b.count - a.count) // 按数量降序
          carTypeCounts : originalData
        });

        if (this.data.typeC) {
          this.setData({
            typeC: false
          })
        }
      }
    })
  },
  onTypeTap(e) {
    const CLIENT_NO = e.currentTarget.dataset.client_no;
    wx.navigateTo({
      url: '../carRealtime/carRealtime?CLIENT_NO=' + CLIENT_NO
    });
  },

  openTypeC() {
    console.log(this.data.clientNoList)
    this.setData({
      typeC: true
    })
  },


  selectClientNoChange(e) {
    const index = e.detail.value;
    const ClientName = this.data.clientNoList[index].label;
    this.setData({
      index,
      ClientName
    });
  },

  getDateTimeChange() {
    this.getCarRealtimeInfo(this.data.activeTab)
  },
})